-- =====================================================================
-- {{{ s_bindings_mouse
-- =====================================================================
root.buttons(awful.util.table.join(
    awful.button({ }, 3, function () mymainmenu:toggle() end)
--    awful.button({ }, 4, awful.tag.viewnext),
--    awful.button({ }, 5, awful.tag.viewprev)
))
-- }}}

-- =====================================================================
-- {{{ s_bindings_keyboard
-- =====================================================================
-- Keycodes for Microsoft Natural 4000 keyboard
keycodes = {}
-- XF86 keys
keycodes['xf86sleep'] = '#150'
keycodes['xf86mail'] = '#223'
keycodes['xf86www'] = '#158'
keycodes['xf86brightnessdown'] = '#232'
keycodes['xf86brightnessup'] = '#233'
keycodes['xf86display'] = '#235'
keycodes['nosymbol'] = '#191'
keycodes['xf86audiomute'] = '#121'
keycodes['xf86audiolowervolume'] = '#122'
keycodes['xf86audioraisevolume'] = '#123'
keycodes['xf86audionext'] = '#171'
keycodes['xf86audioplay'] = '#172'
keycodes['xf86audioprev'] = '#173'
keycodes['xf86audiostop'] = '#174'
-- Microsoft natural 2000 media buttons
keycodes['mediahome'] = '#180'
keycodes['search'] = '#225'
keycodes['mail'] = '#163'
keycodes['favourites'] = '#164'
keycodes['favourite1'] = '#192'
keycodes['favourite2'] = '#193'
keycodes['favourite3'] = '#194'
keycodes['favourite4'] = '#195'
keycodes['favourite5'] = '#196'
keycodes['mute'] = '#121'
keycodes['calculator'] = '#148'
-- Control buttons
keycodes['escape'] = '#9'
keycodes['tab'] = '#23'
keycodes['caps_lock'] = '#66'
keycodes['shift_l'] = '#50'
keycodes['shift_r'] = '#62'
keycodes['ctrl_l'] = '#37'
keycodes['ctrl_r'] = '#105'
keycodes['win_l'] = '#133'
keycodes['alt_l'] = '#64'
keycodes['alt_r'] = '#108'
keycodes['menu'] = '#135'
keycodes['enter'] = '#36'
keycodes['printscreen'] = '#107'
keycodes['pause'] = '#127'


-- "F Lock" enabled
keycodes['f1'] = '#67'
keycodes['f2'] = '#68'
keycodes['f3'] = '#69'
keycodes['f4'] = '#70'
keycodes['f5'] = '#71'
keycodes['f6'] = '#72'
keycodes['f7'] = '#73'
keycodes['f8'] = '#74'
keycodes['f9'] = '#75'
keycodes['f10'] = '#76'
keycodes['f11'] = '#95'
keycodes['f12'] = '#96'
-- "F Lock" disabled

-- Regular buttons
keycodes['q'] = '#24'
keycodes['w'] = '#25'
keycodes['e'] = '#26'
keycodes['r'] = '#27'
keycodes['t'] = '#28'
keycodes['y'] = '#29'
keycodes['u'] = '#30'
keycodes['i'] = '#31'
keycodes['o'] = '#32'
keycodes['p'] = '#33'
keycodes['['] = '#34'
keycodes[']'] = '#35'
keycodes['a'] = '#38'
keycodes['s'] = '#39'
keycodes['d'] = '#40'
keycodes['f'] = '#41'
keycodes['g'] = '#42'
keycodes['h'] = '#43'
keycodes['j'] = '#44'
keycodes['k'] = '#45'
keycodes['l'] = '#46'
keycodes[';'] = '#47'
keycodes['\''] = '#48'
keycodes['\\'] = '#51'
keycodes['z'] = '#52'
keycodes['x'] = '#53'
keycodes['c'] = '#54'
keycodes['v'] = '#55'
keycodes['b'] = '#56'
keycodes['n'] = '#57'
keycodes['m'] = '#58'
keycodes[','] = '#59'
keycodes['.'] = '#60'
keycodes['/'] = '#61'
keycodes['~'] = '#49'

globalkeys = awful.util.table.join(
--    awful.key({ modkey,           }, "Left",   awful.tag.viewprev       ),
--    awful.key({ modkey,           }, "Right",  awful.tag.viewnext       ),
    awful.key({ modkey,           }, keycodes['~'], awful.tag.history.restore),

    awful.key({ modkey,           }, keycodes['j'],
        function ()
            awful.client.focus.byidx( 1)
            if client.focus then client.focus:raise() end
        end),
    awful.key({ modkey,           }, keycodes['k'],
        function ()
            awful.client.focus.byidx(-1)
            if client.focus then client.focus:raise() end
        end),
    awful.key({ modkey,           }, keycodes['w'], function () mymainmenu:show(true)        end),

    -- Layout manipulation
    awful.key({ modkey, "Shift"   }, keycodes['j'], function () awful.client.swap.byidx(  1)    end),
    awful.key({ modkey, "Shift"   }, keycodes['k'], function () awful.client.swap.byidx( -1)    end),
    awful.key({ modkey, "Control" }, keycodes['j'], function () awful.screen.focus_relative( 1) end),
    awful.key({ modkey, "Control" }, keycodes['k'], function () awful.screen.focus_relative(-1) end),
    awful.key({ modkey,           }, keycodes['u'], awful.client.urgent.jumpto),
    awful.key({ modkey,           }, 'Tab',
        function ()
            awful.client.focus.history.previous()
            if client.focus then
                client.focus:raise()
            end
        end),

    -- Standard program
    awful.key({ modkey,           }, 'Return', function () awful.util.spawn(terminal) end),
    awful.key({ modkey, "Control" }, keycodes['r'], awesome.restart),
    awful.key({ modkey, "Control" }, keycodes['q'], awesome.quit),

    awful.key({ modkey,           }, keycodes['l'],     function () awful.tag.incmwfact( 0.05)    end),
    awful.key({ modkey,           }, keycodes['h'],     function () awful.tag.incmwfact(-0.05)    end),
    awful.key({ modkey, "Shift"   }, keycodes['h'],     function () awful.tag.incnmaster( 1)      end),
    awful.key({ modkey, "Shift"   }, keycodes['l'],     function () awful.tag.incnmaster(-1)      end),
    awful.key({ modkey, "Control" }, keycodes['h'],     function () awful.tag.incncol( 1)         end),
    awful.key({ modkey, "Control" }, keycodes['l'],     function () awful.tag.incncol(-1)         end),
    awful.key({ modkey,           }, 'space', function () awful.layout.inc(layouts,  1) end),
    awful.key({ modkey, "Shift"   }, 'space', function () awful.layout.inc(layouts, -1) end),

    -- User bindings
    awful.key({ "Mod1", "Shift" }, '7', function () awful.util.spawn("setxkbmap us") end),
    awful.key({ "Mod1", "Shift" }, '8', function () awful.util.spawn("setxkbmap ru") end),
    awful.key({ "Mod1", "Shift" }, '9', function () awful.util.spawn("setxkbmap ua") end),
    awful.key({ modkey }, "]", function () wi_counter.text = wi_counter.text + 1 end),
    awful.key({ modkey }, "[", function () wi_counter.text = 0 end),
    awful.key({ }, keycodes['xf86sleep'], function () awful.util.spawn("sys.hibernate") end),
    awful.key({ }, keycodes['nosymbol'], function () awful.util.spawn("lock_screen") end),
    

    -- MPD
    awful.key({ }, keycodes['xf86audioplay'], function () awful.util.spawn("mpc toggle") end),
    awful.key({ }, keycodes['xf86audionext'], function () awful.util.spawn("mpc next") end),
    awful.key({ }, keycodes['xf86audioprev'], function () awful.util.spawn("mpc prev") end),
    awful.key({ }, keycodes['xf86audiostop'], function () awful.util.spawn("mpc stop") end),
    awful.key({ "Control" }, keycodes['xf86audionext'], function () awful.util.spawn("mpc seek +10") end),
    awful.key({ "Control" }, keycodes['xf86audioprev'], function () awful.util.spawn("mpc seek -10") end),
    awful.key({ }, keycodes['xf86audiolowervolume'], function () awful.util.spawn("mpc volume -2") end),
    awful.key({ }, keycodes['xf86audioraisevolume'], function () awful.util.spawn("mpc volume +2") end),
    awful.key({ }, keycodes['xf86audiomute'], function () awful.util.spawn("mpc volume 0") end),

    -- Prompt
    awful.key({ modkey },            "r",     function () awful.util.spawn("sys.zshrun") end)
--[[
    awful.key({ modkey }, "x",
              function ()
                  awful.prompt.run({ prompt = "Run Lua code: " },
                  mypromptbox[mouse.screen].widget,
                  awful.util.eval, nil,
                  awful.util.getdir("cache") .. "/history_eval")
              end)
--]]
)

clientkeys = awful.util.table.join(
    awful.key({ modkey,           }, keycodes['f'],      function (c) c.fullscreen = not c.fullscreen  end),
    awful.key({ modkey, 	}, keycodes['escape'],      function (c) c:kill()                         end),
    awful.key({ modkey, "Control" }, 'space',  awful.client.floating.toggle                     ),
    awful.key({ modkey, "Control" }, 'Return', function (c) c:swap(awful.client.getmaster()) end),
    awful.key({ modkey,           }, keycodes['o'],      awful.client.movetoscreen                        ),
    awful.key({ modkey, "Shift"   }, keycodes['r'],      function (c) c:redraw()                       end),
--    awful.key({ modkey,           }, "n",      function (c) c.minimized = not c.minimized    end),
    awful.key({ modkey,           }, keycodes['m'],
        function (c)
            c.maximized_horizontal = not c.maximized_horizontal
            c.maximized_vertical   = not c.maximized_vertical
            c:raise()
        end)
)

-- Compute the maximum number of digit we need, limited to 9
keynumber = 0
for s = 1, screen.count() do
   keynumber = math.min(9, math.max(#tags[s], keynumber));
end
-- Bind all key numbers to tags.
-- Be careful: we use keycodes to make it works on any keyboard layout.
-- This should map on the top row of your keyboard, usually 1 to 9.
for i = 1, keynumber do
    globalkeys = awful.util.table.join(globalkeys,
        awful.key({ modkey }, "#" .. i + 9,
                  function ()
                        local screen = mouse.screen
                        if tags[screen][i] then
                            awful.tag.viewonly(tags[screen][i])
                            mouse.coords({x=0,y=0}, false)
                        end
                  end),
        awful.key({ modkey, "Control" }, "#" .. i + 9,
                  function ()
                      local screen = mouse.screen
                      if tags[screen][i] then
                          awful.tag.viewtoggle(tags[screen][i])
                      end
                  end),
        awful.key({ modkey, "Shift" }, "#" .. i + 9,
                  function ()
                      if client.focus and tags[client.focus.screen][i] then
                          awful.client.movetotag(tags[client.focus.screen][i])
                      end
                  end),
        awful.key({ modkey, "Control", "Shift" }, "#" .. i + 9,
                  function ()
                      if client.focus and tags[client.focus.screen][i] then
                          awful.client.toggletag(tags[client.focus.screen][i])
                      end
                  end))
end

clientbuttons = awful.util.table.join(
    awful.button({ }, 1, function (c) client.focus = c; c:raise() end),
    awful.button({ modkey }, 1, awful.mouse.client.move),
    awful.button({ modkey }, 3, awful.mouse.client.resize))

-- Set keys
root.keys(globalkeys)
-- }}}
